package com.laobiao.file_netty_lib.netty.client.handler;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.laobiao.file_netty_lib.content.LoggerUtil;
import com.laobiao.file_netty_lib.netty.client.ClientBusiness;
import com.laobiao.file_netty_lib.netty.client.FileClient;
import com.laobiao.file_netty_lib.packet.BasePacket;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class ClientHandler extends SimpleChannelInboundHandler<BasePacket> {
	private static final Logger logger=LoggerUtil.getLogger(ClientHandler.class);
	/**
	 * 连接
	 */
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		logger.info("联通服务端:"+ctx.channel().remoteAddress().toString());
	}

	/**
	 * 有数据来了
	 */
	@Override
	public void channelRead0(ChannelHandlerContext ctx, BasePacket msg) throws Exception {
		// TODO Auto-generated method stub
		logger.log(Level.CONFIG,"客户端收到反馈数据"+msg);
		String remoteAddress = ctx.channel().remoteAddress().toString();//远程连接地址
		int businessGroup = msg.getBusinessGroup();
		ClientBusiness packet = FileClient.getCommandBusiness(remoteAddress, businessGroup);//取业务类
		if(packet!=null) {
			packet.resoultPacket(msg);
		}
	}

	/**
	 * 异常
	 */
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		logger.warning("客户端异常:" + cause.getMessage());
	}
}
